<?php

require_once (__DIR__ . '/MOCLASS/account.model.php');
require_once (__DIR__ . '/CommonClass/errorcode.php');

class AccountController {
	public function requestHandler($arr) {
		$output = array(ErrorCode::failRead);

		$accountMO = Account::find($arr);
		//        print_r($accountMO);
		if (!empty($accountMO)) {

			if (!empty($arr[':password'])) {
				$output = $accountMO[0]->password;
			} else if (!empty($arr[':empStatus'])) {
				$output = $accountMO[0] -> empStatus;
			} else if (!empty($arr[':accName'])) {
				//One Account
				$digest = hash('md5', ($accountMO[0]->accName) . ($accountMO[0]->password));
				$item = array(
						'accName'  => $accountMO[0]->accName,
						'dataHash' => $digest
				);
				$output = array('errorCode' => ErrorCode::successRead, 'results' => $item);
			} else if (empty($arr)) {
				//Many Accounts
				$output = array('errorCode' => ErrorCode::successRead);
				$dataArr = array();
				foreach ($accountMO as $key => $account) {
					$digest = hash('md5', ($account->accName) . ($account->password));
					$item = array(
							'accName' => $account -> accName,
							'dataHash' => $digest
					);
					$dataArr[] = $item;
				}
				$output['results'] = $dataArr;
			}
		}

		return $output;
	}

	public function createHandler($arr) {

		$create = Account::create($arr);
		if ($create == 0) {
			return array(ErrorCode::failCreate);
		}

		return array(ErrorCode::successCreate);
	}

	public function deleteHandler($arr) {
		//dashboard

		$accName = $arr[':accName'];
		$dataHash = $arr['dataHash'];

		$valid = $this->integrityValidator($accName, $dataHash);
		if ($valid != ErrorCode::successRead) {
			return array($valid);
		}

		$deletion = Account::delete(array(':accName' => $accName));
		if ($deletion == 0) {
			return array(ErrorCode::failDelete);
		}

		return array(ErrorCode::successDelete);
	}

	public function updateHandler($arr) {
		//dashboard
		$accName = $arr[':accName'];
		$accPass = $arr[':password'];
		$dataHash = $arr['dataHash'];

		$valid = $this->integrityValidator($accName, $dataHash);
		if ($valid != ErrorCode::successRead) {
			return array($valid);
		}

		$update = Account::update(array(':accName' => $accName, ':password' => $accPass));
		if ($update == 0) {
			return array(ErrorCode::failUpdate);
		}

		return array(ErrorCode::successUpdate);
	}

	private function integrityValidator($accName, $dataHash) {
		//account existence check
		$arr = array(':accName' => $accName);
		echo '<script>console.log("Raw $dataHash: '. $dataHash .'");</script>';
		$accountcheck = $this->requestHandler($arr);

		if ($accountcheck['errorCode'] != ErrorCode::successRead) {
			return ErrorCode::integFailCreate;
		}

		if ($accountcheck['results']['dataHash'] != $dataHash){
			//        if ($digest != $dataHash) {
			return ErrorCode::integFailCreate;
		}

		return ErrorCode::successRead;
	}

	public function authenticateValidator($authUsername, $authPass) {
		$password = $this->requestHandler(array(':accName' => $authUsername, ':password' => $authPass));

		if ($password == $authPass) {
			return true;
		} else {
			return false;
		}
	}

	public function managerValidator($authUsername) {
		$status = $this->requestHandler(array(':accName' => $authUsername, ':empStatus' => 'Manager'));
		if ($status == 'Manager') {
			return true;
		} else {
			return false;
		}
	}

}

?>
